package com.mark.springsecurity.dto.resp;

import com.mark.springsecurity.domain.SysUser;
import com.mark.springsecurity.dto.req.vo.SysRoleVo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;

import java.util.Set;
import java.util.stream.Collectors;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class SysUserDto {

    private Long id;

    private String realName;

    private String username;

    private String password;

    private String role;

    private Set<SysRoleVo> roleVos;

    public static Page<SysUserDto> buildListWithPage(Page<SysUser> sysUsers) {
        return sysUsers.map(SysUserDto::new);
    }

    public SysUserDto(SysUser sysUser) {
        this.id = sysUser.getId();
        this.realName = sysUser.getRealName();
        this.username = sysUser.getUsername();
        this.password = sysUser.getPassword();
        this.role = sysUser.getRole();
    }

    public static SysUserDto build(SysUser sysUser) {
        return SysUserDto.builder()
                .id(sysUser.getId())
                .username(sysUser.getUsername())
                .realName(sysUser.getRealName())
                .role(sysUser.getRole())
                .password(sysUser.getPassword())
                .roleVos(sysUser.getRoles().stream().map(SysRoleVo::build).collect(Collectors.toSet()))
                .build();
    }
}
